﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reactive;
using System.Reactive.Disposables;
using System.Reactive.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using ReactiveUI;

namespace RxWPF.Views
{
    public class RoutingView2er : ReactiveObject,IRoutableViewModel,IActivatableViewModel
    {
        public ViewModelActivator Activator { get; } = new ViewModelActivator();
        public static string Path = "RoutingView2er";
        public RoutingView2er(IScreen host)
        {
            this.HostScreen = host;

            Back = ReactiveCommand.CreateFromTask(
                async () =>await HostScreen.Router.Navigate.Execute(
                    new RoutingViewer(HostScreen)).Select(x=> Unit.Default));

            this.WhenActivated(d =>
            {
                Observable.Interval(TimeSpan.FromSeconds(1))
                .ObserveOn(RxApp.MainThreadScheduler)
                .Subscribe(i =>
                {
                    Count++;
                    Debug.WriteLine($"{this.GetHashCode()} is runnint timer {Count}");
                }).DisposeWith(d);
            });
           
        }

        public string UrlPathSegment => Path;

        public IScreen HostScreen { get; set; }

        public ReactiveCommand<Unit, Unit> Back { get; }

        private int count;
        public int Count
        {
            get => count;
            set => this.RaiseAndSetIfChanged(ref count, value);
        }
    }
    public class RoutingView2Base:ReactiveUserControl<RoutingView2er>
    {

    }
    /// <summary>
    /// RoutingView2.xaml 的交互逻辑
    /// </summary>
    public partial class RoutingView2 
    {
        public RoutingView2()
        {
            InitializeComponent();

            this.WhenActivated(d =>
            {
                d(this.BindCommand(ViewModel, vm => vm.Back, view => view.backBtn));
                d(this.Bind(ViewModel, vm => vm.Count, view => view.tbCount.Text));
            });
        }
    }
}
